/*************************************************************************
  Crytek Source File.
  Copyright (C), Crytek Studios, 2001-2010.
 -------------------------------------------------------------------------
  $Id$
  $DateTime$
  
 -------------------------------------------------------------------------
  History:
  - Oct 28th, 2010  : Created by Michelle Martin
 *************************************************************************/

#include "StdAfx.h"
#include "IKTarget_Bone.h"
#include <IEntitySystem.h>  // for GetEntity() by id
#include <ICryAnimation.h>  // for Character and SkeletonPose interfaces to access bone information



CIKTarget_Bone::CIKTarget_Bone(const SIKTargetParams& ikParams, QuatT &startPosition, EntityId targetEntityID, uint boneId) : CIKTarget_Base(ikParams, startPosition)
{
	m_targetEntity = gEnv->pEntitySystem->GetEntity(targetEntityID);
	if (!m_targetEntity)
		CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "IK Target: Invalid Entity ID for Attachment Target, this will cause the IK to crash when used with this");

	ICharacterInstance* pCharacterInstance = m_targetEntity->GetCharacter(0);
	if (!pCharacterInstance)
		CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_ERROR, "IK Target: Entity doesn't have a character in slot 0. Cannot access bone information.");

	m_boneId = boneId;
}

// ===========================================================================

QuatT CIKTarget_Bone::UpdateWorldPos( float frameTime )
{
	ICharacterInstance* pCharacterInstance = m_targetEntity->GetCharacter(0);
	CRY_ASSERT_MESSAGE(pCharacterInstance, "IK Target: Character Instance invalid. Will crash.");

	ISkeletonPose*			pSkeletonPose = pCharacterInstance->GetISkeletonPose();
	CRY_ASSERT_MESSAGE(pSkeletonPose, "IK Target: Skeleton Pose interface invalid. Will crash.");

	return QuatT(m_targetEntity->GetSlotWorldTM(0)) *  pSkeletonPose->GetAbsJointByID(m_boneId);
}


// ===========================================================================






